package com.lagou.edu.controller;

import com.lagou.edu.dao.ResumeDao;
import com.lagou.edu.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("lagou")
public class LoginController {
    @Autowired
    private ResumeDao resumeDao;

    /*@RequestMapping("login")
    public ModelAndView login(@RequestParam("username") String username,
                         @RequestParam("password") String password) {
        ModelAndView mv = new ModelAndView();
        if ("admin".equals(username) && "admin".equals(password)) {
            // 查询数据库中的信息
            List<Resume> list = resumeDao.findAll();
            mv.addObject("resumes",list);
            mv.setViewName("resume");
        }else{
            mv.setViewName("login");
        }
        return mv;
    }*/

    @RequestMapping("login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        if ("admin".equals(username) && "admin".equals(password)) {
            // 查询数据库中的信息
            List<Resume> list = resumeDao.findAll();
            request.setAttribute("resumes",list);
            return "resume";
        }else{
            return "login";
        }
    }

    @RequestMapping("tologin")
    public String tologin() {
        return "login";
    }

}
